<template>
  <div class="reward-punishment-list">
    <div class="header-actions">
      <router-link to="/main/renshi/kaoqing/jiangchen/jiangchengtb" class="action-link">返回</router-link>
      <router-link to="/main/renshi/kaoqing/jiangchen/jiangchengtb" class="action-link">新增申请</router-link>
    </div>
    <h2 class="page-title">奖惩提报单列表</h2>
    <el-table :data="tableData" border stripe style="width: 100%;" class="custom-table">
      <el-table-column prop="employeeId" label="工号" width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="departmentName" label="部门名称" width="120"></el-table-column>
      <el-table-column prop="rewardPunishmentType" label="奖惩类别" width="calc(100% - 120)"></el-table-column>
      <el-table-column prop="reason" label="事由" width="calc(100% - 200)"></el-table-column>
      <el-table-column prop="amount" label="金额" width="130">
        <template slot-scope="scope">
          {{ scope.row.amount.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="date" label="奖惩日期" width="130">
        <template slot-scope="scope">
          {{ formatDate(scope.row.date) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleDetail(scope.row)" class="action-button">详情</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)" class="action-button">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)" class="action-button">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total" class="custom-pagination">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="奖惩提报单详情" :visible.sync="dialogVisible" width="50%" class="custom-dialog">
      <div v-if="detailData" class="dialog-content">
        <p><strong>工号：</strong>{{ detailData.employeeId }}</p>
        <p><strong>姓名：</strong>{{ detailData.name }}</p>
        <p><strong>部门名称：</strong>{{ detailData.departmentName }}</p>
        <p><strong>奖惩类别：</strong>{{ detailData.rewardPunishmentType }}</p>
        <p><strong>事由：</strong>{{ detailData.reason }}</p>
        <p><strong>金额：</strong>{{ detailData.amount }}</p>
        <p><strong>职务：</strong>{{ detailData.position }}</p>
        <p><strong>奖惩日期：</strong>{{ formatDate(detailData.date) }}</p>
        <p><strong>备注：</strong>{{ detailData.remark }}</p>
        <p><strong>批准人：</strong>{{ detailData.approvalPerson }}</p>
        <p><strong>审核人：</strong>{{ detailData.reviewPerson }}</p>
        <p><strong>制作人：</strong>{{ detailData.maker }}</p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRewardPunishmentReportList, getRewardPunishmentReportDetail, deleteRewardPunishmentReport } from '@/api/rewardPunishmentReport';

export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      detailData: null
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      try {
        const response = await getRewardPunishmentReportList({
          current: this.currentPage,
          size: this.pageSize
        });
        this.tableData = response.data.records;
        this.total = response.data.total;
      } catch (error) {
        this.$message.error('获取数据失败：' + error.message);
      }
    },
    async handleDetail(row) {
      try {
        const response = await getRewardPunishmentReportDetail(row.id);
        this.detailData = response.data;
        this.dialogVisible = true;
      } catch (error) {
        this.$message.error('获取详情失败：' + error.message);
      }
    },
    handleEdit(row) {
      this.$router.push(`/main/renshi/kaoqing/jiangchen/jiangchengtb?id=${row.id}`);
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该奖惩提报单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        await deleteRewardPunishmentReport(row.id);
        this.$message.success('删除成功');
        this.fetchData();
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('删除失败：' + error.message);
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    formatDate(date) {
      if (!date) return '';
      return date.split('T')[0];
    }
  }
};
</script>

<style scoped>
.reward-punishment-list {
  font-family: '宋体', sans-serif;
  padding: 20px;
  /* background-color: #f9f9f9; */
  border-radius: 8px;
  /* box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); */
  width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
}

.header-actions {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.action-link {
  color: #409eff;
  text-decoration: none;
  transition: color 0.3s ease;
}

.action-link:hover {
  color: #2c7be5;
}

.page-title {
  color: #333;
  margin-bottom: 20px;
}

.custom-table {
  border-radius: 8px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

.el-table__header th {
  background-color: #f2f2f2;
}

.el-table__body tr:hover {
  background-color: #f5f5f5;
}

.action-button {
  border-radius: 4px;
}

.custom-pagination {
  margin-top: 20px;
  text-align: right;
}

.custom-dialog {
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.dialog-content {
  padding: 20px;
}
</style>